<template>
  <div class="xd-category">
      <div class="list" v-show="isShow">
        <Card>
          <p slot="title">
            <Icon type="md-list" />
            价格类别列表
          </p>
          <Form ref="formInline" :model="formSearch"  inline>
              <FormItem prop="depaid" v-show="this.access!=='wash'">
                <Select class="select" v-model="formSearch.wdepaid" placeholder="请选择洗涤公司">
                  <Option v-for="item in washList" :value="item.depaid" :key="item.depaid">
                  {{ item.depaname }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem prop="user">
                <Select class="select" placeholder="请选择医院" v-model="formSearch.depaid">
                  <Option v-for="item in hospitalList" :value="item.depaid" :key="item.depaid">
                  {{ item.depaname }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem>
                <Button type="primary" @click="handleSeacrh()">查询</Button>
              </FormItem>
              <FormItem v-show="access == 'wash'">
                <Button type="primary" @click="handleAdd()">新增价格类别</Button>
              </FormItem>
          </Form>
          <Table highlight-row stripe :columns="columns" :data="data" ></Table>
          <div style="margin: 10px;overflow: hidden">
            <div style="float: right;">
              <Page :total="page.total" :page-size="page.pageSize" :current="1" @on-change="changePage" show-total></Page>
            </div>
          </div>
          <Modal
            v-model="modal"
            footer-hide="true"
            transfer="false"
            >
            <p slot="header">
              <Icon type="ios-paper" />
              <span>查看详情</span>
            </p>
            <p class="viewLine"><span>序号:</span> {{currentData.id}}</p>
            <p class="viewLine"><span>医院名称:</span>{{currentData.depaname}}</p>
            <p class="viewLine"><span>类别编号:</span>{{currentData.categoryid}}</p>
            <p class="viewLine"><span>类别名称:</span>{{currentData.categoryname}}</p>
            <p class="viewLine"><span>单价:</span>{{parseFloat(currentData.price).toFixed(2)}}</p>
          </Modal>
        </Card>
      </div>
      <div class="add" v-show="!isShow">
        <Card>
          <p slot="title">
            <Icon type="ios-create" v-show="type!=='edit'"/>
            <Icon type="ios-document" v-show="type=='edit'"/>
            {{type == "edit" ? "编辑价格类别" : "新增价格类别"}}
          </p>
          <p slot="extra" >
            <Button type="primary" @click="goBack()">返回</Button>
          </p>
          <div class="editForm">
            <Form ref="editFormCategory" :model="editFormInLine" :rules="editFormRuleInline" :label-width="80">
              <FormItem label="医院名称" prop="depaid" placeholder="请选择医院">
                  <Select class="select" v-model="editFormInLine.depaid" disabled v-show="type=='edit'">
                    <Option v-for="item in hospitalList" :value="item.depaid" :key="item.depaid">
                    {{ item.depaname }}
                    </Option>
                  </Select>
                  <Select class="select" v-model="editFormInLine.depaid" v-show="type!=='edit'">
                    <Option v-for="item in hospitalList" :value="item.depaid" :key="item.depaid">
                    {{ item.depaname }}
                    </Option>
                  </Select>
              </FormItem>
              <FormItem label="类别名称" prop="name">
                <Input v-model="editFormInLine.name" placeholder="请输入类别名称"></Input>
              </FormItem>
              <FormItem label="基础价格" prop="cprice">
                <Input v-model="editFormInLine.cprice" placeholder="请输入单价(元)"></Input>
              </FormItem>
              <FormItem>
                <Button @click="handleSubmit" type="primary" class="button">提交</Button>
                <Button @click="handleReset" type="primary" class="button">重置</Button>
              </FormItem>
            </Form>
          </div>
        </Card>
      </div>
    </Card>
  </div>
</template>

<script>
import API from "./api";
import ValidatorRule from "@/filters/validator-rule";
import i18n from "./locale";
import mixinTag from "@/mixin/tag-mixin";

export default {
  name: "xd-category",
  components: {},
  mixins: [mixinTag],
  data() {
    return {
      i18n: i18n,
      modal: false,
      access: "",
      isShow: true,
      currentData: "",
      hospitalList: [],
      washList: [],
      type: "",
      editFormInLine: {
        depaid: "",
        name: "",
        cprice: ""
      },
      editFormRuleInline: {
        depaid: ValidatorRule.selectNum,
        name: ValidatorRule.require,
        cprice: ValidatorRule.price
      },
      page: {
        total: 0,
        pageSize: 15
      },
      formSearch: {
        wdepaid: "",
        depaid: "",
        pageNum: 1,
        pageSize: 15
      },
      columns: [
        {
          title: "序号",
          key: "id"
        },
        {
          title: "医院名称",
          key: "depaname"
        },
        {
          title: "类别编号",
          key: "categoryid"
        },
        {
          title: "类别名称",
          key: "categoryname"
        },
        {
          title: "单价(元)",
          key: "price",
          render: (h, params) => {
            const row = params.row;
            return h("span", parseFloat(row.price).toFixed(2));
          }
        },
        {
          title: "操作",
          key: "action",
          width: 200,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.show(params.index);
                    }
                  }
                },
                "查看"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small"
                  },
                  style: {
                    marginRight: "5px"
                  },
                  on: {
                    click: () => {
                      this.edit(params.index);
                    }
                  }
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small"
                  },
                  on: {
                    click: () => {
                      this.delete(params.index, params.row.id);
                    }
                  }
                },
                "删除"
              )
            ]);
          }
        }
      ],
      data: []
    };
  },

  mounted() {
    this.getHospitalList();
    this.resetInit();
  },

  activated() {
    this.getHospitalList();
    let userInfo = this.$store.state.user.userInfo.data;
    this.access = this.$store.state.user.access;
    if (this.access !== "wash") {
      this.getWashlList();
    }
  },

  methods: {
    resetInit() {
      let userInfo = this.$store.state.user.userInfo.data;
      this.access = this.$store.state.user.access;
      if (this.access == "wash") {
        this.formSearch.depaid = userInfo.depaid;
        this.handleSeacrh();
      } else {
        this.getWashlList();
      }
    },

    getWashlList() {
      API.getWashlList({ type: 1 })
        .then(res => {
          if (res.code == 200) {
            this.washList = res.data;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    getHospitalList() {
      API.getHospitalList({ type: 2 })
        .then(res => {
          if (res.code == 200) {
            this.hospitalList = res.data;
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    handleSeacrh() {
      let obj = this.formSearch;
      if (obj.depaid == "") {
        this.$Message.error("医院必填！");
        return;
      }
      obj.pageNum = 1;
      this.formSearch = obj;
      this.getList(obj);
    },

    changePage(pageNum) {
      let obj = this.formSearch,
        maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      if (obj.pageNum > maxPageNum) {
        obj.pageNum = maxPageNum;
      } else {
        obj.pageNum = pageNum;
      }
      this.formSearch = obj;
      this.getList(obj);
    },

    getList(obj) {
      API.getList(obj)
        .then(res => {
          let data = res.data;
          this.page.total = data.total;
          this.page.pageSize = data.pageSize;
          this.data = data.list;
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    },

    handleAdd() {
      this.isShow = false;
      this.type = "add";
      this.currentData = "";
      this.handleReset();
    },

    show(index) {
      this.currentData = this.data[index];
      this.modal = true;
    },

    edit(index) {
      this.isShow = false;
      this.type = "edit";
      this.currentData = this.data[index];
      //赋值
      this.editFormInLine.depaid = this.currentData.depaid;
      this.editFormInLine.name = this.currentData.categoryname;
      this.editFormInLine.cprice = this.currentData.price.toFixed(2).toString();
    },

    resetList() {
      //求余为1，页码为最大页码，请求页需减1
      let maxPageNum = Math.ceil(this.page.total / this.page.pageSize);
      let remainderNum = this.page.total % this.page.pageSize;
      if (
        this.formSearch.pageNum == maxPageNum &&
        remainderNum == 1 &&
        this.formSearch.pageNum !== 1
      ) {
        this.formSearch.pageNum = this.formSearch.pageNum - 1;
      }
      let obj = this.formSearch;
      this.getList(obj);
    },

    delete(index, id) {
      let tthis = this;
      this.$Modal.confirm({
        title: "提示：",
        content: "你确定删除这条记录",
        onOk: function() {
          let obj = {
            id: id,
            type: "category"
          };
          API.delete(obj)
            .then(res => {
              if (res.code == 200) {
                this.$Message.success({ content: "该记录已经删除成功！" });
                tthis.data.splice(index, 1);
                tthis.resetList();
              }
            })
            .catch(err => {
              this.$Message.error("请求异常！");
            });
        }
      });
    },

    goBack() {
      this.isShow = true;
      this.handleReset();
    },

    handleReset() {
      this.$refs.editFormCategory.resetFields();
      if (this.type == "edit") {
        this.editFormInLine.depaid = this.currentData.depaid;
      }
    },

    handleSubmit() {
      this.$refs.editFormCategory.validate(valid => {
        if (valid) {
          if (this.type == "edit") {
            this.editSubmit(this.editFormInLine);
          } else {
            this.addSubmit(this.editFormInLine);
          }
        }
      });
    },

    editSubmit(obj) {
      this.formSearch.depaid = obj.depaid;
      obj.id = this.currentData.id;
      obj.id2 = this.currentData.id2;
      API.editSubmit(obj)
        .then(res => {
          this.resetList();
          this.$Message.success("修改成功！");
          this.goBack();
        })
        .catch(err => {
          this.$Message.error("修改异常！");
        });
    },

    addSubmit(obj) {
      this.formSearch.depaid = obj.depaid;
      API.addSubmit(obj)
        .then(res => {
          if (res.code == 200) {
            this.handleSeacrh();
            this.$Message.success("添加成功！");
            this.goBack();
          }
        })
        .catch(err => {
          this.$Message.error("请求异常！");
        });
    }
  }
};
</script>
<style lang="less" >
@import "./xd-category.less";
</style>
